﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRM.DataObjects;
using System.Data;

namespace SRM
{
    public partial class ApplicantForm : System.Web.UI.Page, IApplicantForm
    {
        public DataTable Applicant { get; set; }
        public string ApplicantKey { get; set; }

        public string PreviousApplicant { get; set; }
        public string NextApplicant { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.errorMessage.Text = string.Empty;

            if ((HttpContext.Current.User.Identity.AuthenticationType == "Forms") &&
                ((!Request.IsAuthenticated) || (Session["ApplicantKey"] == null)))
            {
                if (Request.QueryString.Count > 0)
                    Response.Redirect("~/LoginForm.aspx?" + Request.QueryString.ToString());
                else
                    Response.Redirect("~/LoginForm.aspx");
            }

            if (Session["ApplicantKey"] == null)
            {
                if (Request.QueryString.Count > 0)
                    Response.Redirect("~/LoginForm.aspx?" + Request.QueryString.ToString());
                else
                    Response.Redirect("~/LoginForm.aspx");
            }
                

            this.hdn_UseSAPCode.Value = "1";
            this.ApplicantKey = Session["ApplicantKey"].ToString();
            this.hdn_applicantKey.Value = Session["ApplicantKey"].ToString();
            int applicantKey = int.Parse(Session["ApplicantKey"].ToString());

            #region Setup ApplicantKey
            
            if (Request.QueryString["key"] != null)
            {
                if (!int.TryParse(Request.QueryString["key"], out applicantKey))
                {
                    applicantKey = int.Parse(Session["ApplicantKey"].ToString());
                }
                else
                {
                    Session["ApplicantKey"] = applicantKey;
                }
            }

            #endregion Setup ApplicantKey

            DataTable dtPrevNext = SRM_Applicant.GetPrevAndNextApplicant(applicantKey.ToString());
            if (dtPrevNext.Rows.Count > 0)
            {
                if (dtPrevNext.Rows[0]["ApplicantKey"] == DBNull.Value)
                    PreviousApplicant = string.Empty;
                else
                    PreviousApplicant = string.Format("<a href=\"ApplicantForm.aspx?key={0}\">prev</a>", dtPrevNext.Rows[0]["ApplicantKey"].ToString());

                if (dtPrevNext.Rows[1]["ApplicantKey"] == DBNull.Value)
                    NextApplicant = string.Empty;
                else
                    NextApplicant = string.Format("<a href=\"ApplicantForm.aspx?key={0}\">next</a>", dtPrevNext.Rows[1]["ApplicantKey"].ToString());
            }


            #region Setup Position (if any else unsolicited)

            if ((Session["RequisitionKey"] == null) && (Request.QueryString["pos"] == null))
            {
                Session["RequisitionKey"] = SRM_Requisition.GetUnsolicitedRequisitionKey();
            }
            else
            {
                string requisitionKey = string.Empty;

                if ((string.IsNullOrEmpty(requisitionKey)) && (Session["RequisitionKey"] != null))
                    requisitionKey = Session["RequisitionKey"].ToString().Trim();

                if ((string.IsNullOrEmpty(requisitionKey)) && (Request.QueryString["pos"] != null))
                    requisitionKey = Request.QueryString["pos"].Trim();

                Session["RequisitionKey"] = requisitionKey.ToString();

                if (Session["RequisitionKey"].ToString() != SRM_Requisition.GetUnsolicitedRequisitionKey())
                {
                    DataTable dtRequisition = SRM_Requisition.GetRequisition(Session["RequisitionKey"].ToString());

                    if (dtRequisition.Rows.Count > 0)
                    {
                        role_literal.Text = string.Format("<p>You are applying for the role: {0}</p>", dtRequisition.Rows[0]["JobTitleFreeText"].ToString());
                    }

                }
            }

            #endregion Setup Position (if any else unsolicited)

            if (Session["Stage"].ToString() != "2")
            {
                for (int tabIndex = 2; tabIndex < RadTabStrip.Tabs.Count; tabIndex++)
                {
                    RadTabStrip.Tabs[tabIndex].Visible = false;
                }
            }
            if ((Session["Stage"].ToString() == "1") || (Session["RequisitionKey"].ToString() == SRM_Requisition.GetUnsolicitedRequisitionKey()))
            {
                for (int tabIndex = 2; tabIndex < 9; tabIndex++)
                {
                    RadTabStrip.Tabs[tabIndex].Visible = true;
                }
            }

            if (!Page.IsPostBack)
            {
                this.Applicant = SRM_Applicant.GetApplicant(applicantKey);
                if (this.Applicant.Rows.Count > 0)
                {
                    DataRow row = this.Applicant.Rows[0];
                    this.cboDeclaration.Checked = row.Field<DateTime?>("PDPAConsent").HasValue;
                }
            }


            
        }

        private void SaveForm()
        {
            Telerik.Web.UI.RadMultiPage multipage = Help.FindControlRecursive(Page.Controls, "RadMultiPage") as Telerik.Web.UI.RadMultiPage;

            if (multipage == null)
                return;

            foreach (Telerik.Web.UI.RadPageView pageview in multipage.PageViews)
            {
                foreach (Control pvc in pageview.Controls)
                {
                    if (pvc is IUserInput)
                    {
                        ((IUserInput)pvc).SaveUserInput();
                    }
                }
            }

            SRM_Applicant.UpdatePDPAConsent(Session["ApplicantKey"].ToString(), this.cboDeclaration.Checked);

            // Insert into SRM_RequisitionApplicant_Tbl
            SRM_Requisition.InsertApplicant(Session["ApplicantKey"].ToString(), Session["RequisitionKey"].ToString());
        }

        protected void SaveApplicationForm_Button_Click(object sender, EventArgs e)
        {
            this.SaveForm();
        } // protected void SaveApplicationForm_Button_Click

        protected void SubmitApplicationForm_Button_Click(object sender, EventArgs e)
        {
            List<string> personalParticularsErrors = new List<string>();
            List<string> nsErrors = new List<string>();
            List<string> additionalParticulars = new List<string>();
            List<string> furtherInfo = new List<string>();

            this.SaveForm();

            // Check if Form is Complete
            DataTable dt = null;
            DataRow dr = null;
            int applicantKey = int.Parse(Session["ApplicantKey"].ToString());
            SRM_Applicant.GetApplicant(applicantKey);

            bool isValid = true;
            bool formIsComplete = true;
            List<string> problemTabs = new List<string>();
            
            //--We need to check the following tabs:
            //--Personal Particulars
            dt = SRM_Applicant.GetPersonalParticulars(applicantKey.ToString());
            if (dt.Rows.Count > 0)
            {
                isValid = true;
                dr = dt.Rows[0];
                if ((isValid) && (string.IsNullOrEmpty(dr["VacancyMedium"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in how you came to know about this vacancy.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Salutation"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Salutation.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["GivenName"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Given Name.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["LastName"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Last Name.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["NameFormat"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Name Format.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["NRICNo"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Identity No.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["NRICType"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Identity Type.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["NRICIssuingDate"].ToString())))
                {
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Issuing Date.");
                    isValid = false;
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Citizenship"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Citizenship.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["PlaceOfBirth"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Place of Birth.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["StreetName"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Street Name.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["PostalCode"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Postal Code.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["CountryCode"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Country Code.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["City"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in City.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["State"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in state.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["MobileNo"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Contact No (M).");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["HomeNo"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Contact No (H).");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["DrivingClass"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Driving Licence.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["YrsExperience"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill in Year of Experience.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["PeriodNotice"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Period of Notice.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Availability"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Approx Availability Date.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["AskingSalary"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Expected Monthly Salary.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["PdpaConsent"].ToString())))
                {
                    isValid = false;
                    personalParticularsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Consent and Declaration.");
                    
                }

                if (!isValid)
                {
                    string msg = "Under Personal Particulars tab";
                    foreach (string err in personalParticularsErrors)
                    {
                        msg = msg + err;
                    }
                    //problemTabs.Add("Personal Particulars" + personalParticularsErrors.ToString());
                    problemTabs.Add(msg);
                    formIsComplete = false;
                }
            }
            //--Resume File
            if (dt.Rows.Count > 0)
            {
                isValid = true;
                dr = dt.Rows[0];

                if ((isValid) && (string.IsNullOrEmpty(dr["ResumeFileName"].ToString())))
                {
                    isValid = false;
                }

                if (!isValid)
                {
                    problemTabs.Add("Resume"+ " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please upload resume file.");
                    formIsComplete = false;
                }
            }

            //--Education Qualification
            if ((SRM_EducationQualification.GetEducationQualification(applicantKey.ToString()).Rows.Count <= 0) && (RadTabStrip.Tabs[2].Visible))
            {
                isValid = false;

                if (!isValid)
                {
                    problemTabs.Add("Under Education Qualification tab " + " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify your education.");
                    formIsComplete = false;
                }
            }
            else
            {
                DataTable dtEducation = SRM_EducationQualification.GetHighestEducationQualification(applicantKey.ToString());

                if ((dtEducation.Rows.Count <= 0) && (RadTabStrip.Tabs[2].Visible))
                {
                    isValid = false;

                    if (!isValid)
                    {
                        problemTabs.Add("Under Education Qualification tab " + " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify your highest education.");
                        formIsComplete = false;
                    }
                }

                if ((dtEducation.Rows.Count > 1) && (RadTabStrip.Tabs[2].Visible))
                {
                    isValid = false;

                    if (!isValid)
                    {
                        problemTabs.Add("Under Education Qualification tab " + " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify only 1 highest education.");
                        formIsComplete = false;
                    }
                }
            }


            //--Language Proficiency
            if ((SRM_Language.GetLanguageProficiency(applicantKey.ToString()).Rows.Count <= 0) && (RadTabStrip.Tabs[3].Visible))
            {
                isValid = false;

                if (!isValid)
                {
                    problemTabs.Add("Under Language Proficiency tab " + " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify your language proficency.");
                    formIsComplete = false;
                }
            }

            //--Employment History
            if ((dt.Rows[0]["FreshGraduate"].ToString() == string.Empty) && (RadTabStrip.Tabs[6].Visible))
            {
                isValid = false;

                if (!isValid)
                {
                    problemTabs.Add("Under Employment History tab " + " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify your employment history.");
                    formIsComplete = false;
                }
            }
            else
            {
                if ((dt.Rows[0]["FreshGraduate"].ToString().Equals("No", StringComparison.InvariantCultureIgnoreCase)) &&
                    (SRM_EmploymentHistory.GetEmploymentHistory(applicantKey.ToString()).Rows.Count <= 0))
                {
                    isValid = false;

                    if (!isValid)
                    {
                        problemTabs.Add("Under Employment History tab " + " <br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify your employment history.");
                        formIsComplete = false;
                    }
                }
            }
            

            //--National Service
            if ((dt.Rows.Count > 0) && (RadTabStrip.Tabs[7].Visible))
            {
                isValid = true;
                dr = dt.Rows[0];

                //if ((isValid) && (string.IsNullOrEmpty(dr["NS"].ToString())))
                //{
                //    isValid = false;
                //}
                //else
                //{
                //    if (dr["NS"].ToString() != "1")
                //    {
                //        if ((isValid) && (string.IsNullOrEmpty(dr["NSServiceTypeCode"].ToString())))
                //        {
                //            isValid = false;
                //            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill National Service");
                //        }
                //        if ((isValid) && (string.IsNullOrEmpty(dr["NSStatus"].ToString())))
                //        {
                //            isValid = false;
                //            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill NS Status.");
                //        }
                //        if ((isValid) && (string.IsNullOrEmpty(dr["NSVocation"].ToString())))
                //        {
                //            isValid = false;
                //            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Vocation.");
                //        }
                //        if ((isValid) && (string.IsNullOrEmpty(dr["NSServiceRankCode"].ToString())))
                //        {
                //            isValid = false;
                //            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Rank.");
                //        }
                //        if ((isValid) && (string.IsNullOrEmpty(dr["NSORDDate"].ToString())))
                //        {
                //            isValid = false;
                //            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill ORD Date.");
                //        }
                //    }
                //}

                if ((isValid) && (string.IsNullOrEmpty(dr["NSStatus"].ToString())))
                {
                    isValid = false;
                    nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill NSF Status");
                }
                else
                {
                    if ((dr["NSStatus"].ToString() != "NA") && 
                        (dr["NSStatus"].ToString() != "DF") && 
                        (dr["NSStatus"].ToString() != "EX"))
                    {
                        if ((isValid) && (string.IsNullOrEmpty(dr["NSServiceTypeCode"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Service Type");
                        }
                        if ((isValid) && (string.IsNullOrEmpty(dr["NSVocation"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Vocation");
                        }
                        if ((isValid) && (string.IsNullOrEmpty(dr["NSServiceRankCode"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Rank");
                        }
                        if ((isValid) && (string.IsNullOrEmpty(dr["NSEnlistmentDate"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Enlistment Date");
                        }
                    }
                }

                if ((isValid) && (string.IsNullOrEmpty(dr["NS"].ToString())))
                {
                    isValid = false;
                    nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill NSMan Status");
                }
                else
                {
                    if ((dr["NS"].ToString() != "1") &&
                        (dr["NS"].ToString() != "3"))
                    {

                        //NSORDDAte,
                        //RSServiceTypeCode,
                        //RSVocation,
                        //RSServiceRankCode,
                        //RSCompletionDate
                        if ((isValid) && (string.IsNullOrEmpty(dr["NSORDDAte"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill ORD Date");
                        }
                        if ((isValid) && (string.IsNullOrEmpty(dr["RSServiceTypeCode"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Service Type");
                        }
                        if ((isValid) && (string.IsNullOrEmpty(dr["RSVocation"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Vocation");
                        }
                        if ((isValid) && (string.IsNullOrEmpty(dr["RSServiceRankCode"].ToString())))
                        {
                            isValid = false;
                            nsErrors.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Rank");
                        }
                        
                    }
                }

                if (!isValid)
                {
                    string msg = "Under National Service tab";
                    foreach (string err in nsErrors)
                    {
                        msg = msg + err;
                    }
                    problemTabs.Add(msg);
                    formIsComplete = false;
                }
            }
            //--Addition Personal Particulars
            if ((dt.Rows.Count > 0) && (RadTabStrip.Tabs[9].Visible))
            {
                isValid = true;
                dr = dt.Rows[0];

                if ((isValid) && (string.IsNullOrEmpty(dr["DateOfBirth"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Date of Birth");
                    
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Race"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Race");

                }
                if ((isValid) && (string.IsNullOrEmpty(dr["MaritalStatus"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Marital Status");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Gender"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Gender");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["NoOfChildren"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill No of children");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Dialect"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Dialect");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["Religion"].ToString())))
                {
                    isValid = false;
                    additionalParticulars.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please fill Religion");
                }

                if (!isValid)
                {
                    string msg = "Under Additional Personal Particulars tab";
                    foreach (string err in additionalParticulars)
                    {
                        msg = msg + err;
                    }
                    //problemTabs.Add("Personal Particulars" + personalParticularsErrors.ToString());
                    problemTabs.Add(msg);
                    formIsComplete = false;
                }
            }

            


            //--Language Proficiency
            //--Professional Membership
            //--Awards
            //--Employment History
            
            //--Further Information
            if ((dt.Rows.Count > 0) && (RadTabStrip.Tabs[8].Visible))
            {
                isValid = true;
                dr = dt.Rows[0];

                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo1"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 1.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo2"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 2.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo3"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 3.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo4"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 4.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo5"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 5.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo6"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 6.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo7"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 7.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo8"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 8.");
                }
                if ((isValid) && (string.IsNullOrEmpty(dr["FurtherInfo9"].ToString())))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please answer question 9.");
                }

                if (!isValid)
                {
                    string msg = "Under Further Information tab";
                    foreach (string err in furtherInfo)
                    {
                        msg = msg + err;
                    }
                    //problemTabs.Add("Personal Particulars" + personalParticularsErrors.ToString());
                    problemTabs.Add(msg);
                    //problemTabs.Add("Further Information");
                    formIsComplete = false;
                }
            }

            //--Family Details
            if ((dt.Rows.Count > 0) && (RadTabStrip.Tabs[10].Visible))
            {
                isValid = true;
                dr = dt.Rows[0];

                if ((isValid) && (SRM_Family.GetFamily(applicantKey.ToString()).Rows.Count <= 0))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify family member(s).");
                }

                if (!SRM_Applicant.FamilyChildrenCheck(applicantKey.ToString()))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please specify your children to match the number of children specified under Additional Personal Particulars tab.");
                }

                if (!SRM_Applicant.HasEmergencyContact(applicantKey.ToString()))
                {
                    isValid = false;
                    furtherInfo.Add("<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please at least one emergency contact.");
                }


                if (!isValid)
                {
                    string msg = "Under Family Details tab";
                    foreach (string err in furtherInfo)
                    {
                        msg = msg + err;
                    }
                    problemTabs.Add(msg);
                    formIsComplete = false;
                }
            }

            //--Experience Living Abroad
            //--Interest & Hobbies
            //--Skills & Knowledge
            //--Applicant Photo
            dt = SRM_Photo.GetPhoto(applicantKey.ToString());
            if ((dt.Rows.Count <= 0) && (RadTabStrip.Tabs[14].Visible))
            {
                isValid = false;
                
                if (!isValid)
                {
                    problemTabs.Add("Applicant Photo");
                    formIsComplete = false;
                }
            }
            
            if (!formIsComplete)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.AppendLine("<p style='color:red;font-weight:bold;'>System Message: Your application have not been submitted as it is incomplete.<br/>Please fill in the required fields in the following sections: </p>");
                sb.AppendLine("<ol style='color:red;'>");
                foreach (string tabName in problemTabs)
                {
                    sb.AppendLine(string.Format("<li>{0}", tabName));
                }
                
                sb.AppendLine("</ol>");

                this.errorMessage.Text = sb.ToString();
            }
            else
            {
                // Set Form as Complete
                SRM_Applicant.SetFormComplete(applicantKey.ToString());
                Response.Redirect("~/SuccessfulSubmission.aspx");
            }

        } // protected void SubmitApplicationForm_Button_Click(object sender, EventArgs e)

    } // public partial class ApplicantForm : System.Web.UI.Page

} // namespace SRM